package com.eiisan.security.config;

import com.eiisan.security.filter.JwtAuthenticationTokenFilter;
import com.eiisan.security.service.CustomUserDetailsService;
import com.eiisan.security.util.JWTTokenUtil;
import com.eiisan.security.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.List;

/**
 * @author eii
 * @time 2020/11/5 11:25 下午
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    private CustomUserDetailsService customUserDetailsService;
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        List<String> permitAllPathList = securityProperties.getAuth().getPermitAllPath();
        http
                // 禁用 CSRF
                .csrf().disable()
                // 不创建会话
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // 验证Http请求
                .authorizeRequests()
                // 配置文件中的 不控制的地址 permitAll
                .antMatchers(permitAllPathList.toArray(new String[0])).permitAll()
                .anyRequest().authenticated().and()
                // 防止iframe 造成跨域
                .headers().frameOptions().disable().and()
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class).headers().cacheControl()
        ;
    }

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService)
            .passwordEncoder(passwordEncoder());
    }

    /**
     * @see: https://docs.spring.io/spring-security/site/docs/current/reference/html5/#spring-security-crypto-passwordencoders
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


    public static void main(String[] args) {
        PasswordEncoder ps = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        String encode = ps.encode("123456");
        //{bcrypt}$2a$10$mqK.N0qKK6vcdEi74xfUCe2OL4m7UkeugXlCSSHDS/gx777MNiM3i
        System.out.println("encode = " + encode);
    }
}
